package cn.zhentao.dao;

import cn.zhentao.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    //根据id查询数据
    @Select("select * from user where id=#{id}")
    @ResultMap("userMap")
    User findById(int id);

    //插入数据
    @Insert("insert into user values(#{id},#{username},#{sex},#{birthday},#{address})")
    @SelectKey(keyColumn = "id", keyProperty = "id", resultType = Integer.class, before = false
            , statement = {"select last_insert_id()"})
    int addUser(User user);

    //删除数据
    @Delete("delete from user where id=#{id}")
    int delUser(int id);

    //使用注解，结合resultMap实现查询
    @Select("select * from User")
    @Results(
            id = "userMap", value = {
            @Result(id = true, column = "id", property = "userId"),
            @Result(column = "username", property = "userName"),
            @Result(column = "sex", property = "sex"),
            @Result(column = "birthday", property = "birthday"),
            @Result(column = "address", property = "address")
    }
    )
    List<User> findAll();
}
